package com.winning.bdu.core.utils;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: yanxf@winning.com.cn
 * Description: 集合工具类
 * Date 2019/05/30 16:15
 */
public class CollectionUtil {
    /**
     * 按指定大小，分隔集合
     *
     * @param list 目标集合
     * @param len 分隔长度
     *
     * @return
     */
    public static <T> List<List<T>> splitList(List<T> list, int len) {

        if (list == null || list.size() == 0 || len < 1) {
            return null;
        }

        List<List<T>> result = new ArrayList<List<T>>();
        int size = list.size();
        int count = (size + len - 1) / len;

        for (int i = 0; i < count; i++) {
            int end = (i + 1) * len > size ? size : len * (i + 1);
            List<T> subList = list.subList(i * len, end);
            result.add(subList);
        }
        return result;
    }

}
